<template>
	<view class="login-page">
		<view class="header">
			<image class="icon" :src="imgUrl" mode="aspectFit"></image>
		</view>
		<text>登录后即可继续当前操作</text>
		<button class="login-button" @click="getUserInfo">微信getUserInfo</button>
		<button class="login-button" @click="getUserProfile">微信getUserProfile</button>
		<button class="login-button" @click="login">微信login</button>
		<!-- #ifdef MP-WEIXIN -->
		<view class="buttom">
			<button open-type="getPhoneNumber" @getphonenumber="weChatLogin" class="clearBtn">
				<view class="loginType">
					<view class="item">
						<view class="icon"><u-icon size="60" name="weixin-fill" color="rgb(83,194,64)"></u-icon></view>
						微信
					</view>
				</view>
			</button>
		</view>
		<!-- #endif -->
	</view>
</template>

<script>
export default {
	data() {
		return {
			phoneNumberData: '',
			openCode: '',

		}
	},
	created() {

	},
	methods: {
	    login(){
	        let _ = this
            uni.login({
                provider: 'weixin',
                success(loginData) {
                    let openCode = loginData.code
                    uni.setStorageSync('openCode', openCode)
                    console.log(uni.getStorageSync('openCode'))
                },
                fail(err) {
                    console.log('拒绝', err)
                }
            })
		},
		getUserInfo() {
		    let _ = this
			console.log('getUserInfo')
			uni.getUserInfo ({
				provider: 'weixin',
				success: (res) => {
					_.$u.vuex('wxUserInfo', res.userInfo);
					console.log('getUserInfo', res);
					console.log('getUserInfo-ava', res.userInfo.avatarUrl);
				},
			});
		},
        getUserProfile() {
            let _ = this
            console.log('getUserProfile')
			uni.showModal({
				title:'授权登录',
                content: '是否授权登录微信小程序？',
                success: (e) => {
				    if(e.confirm){
                        uni.getUserProfile ({
                            desc: '获取你的昵称、头像、地区及性别',
                            success: (res) => {
                                // _.$u.vuex('wxUserInfo', res.userInfo);
                                console.log('getUserProfile', res);
                            },
                        });
					}
				}
			})
        },
        weChatLogin(e){
            console.log()
            let code= e.detail.code;
            if(code){
                uni.showLoading({title:"登录中....",mask:true})
            }else{
                uni.$u.toast('登录失败')
            }
        },
	}
}
</script>
<style scoped lang="scss">
@import 'index.scss';
</style>
